package com.cc.base;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Collections;
import java.util.List;

/**
 * 分页结果包装
 * @author shadow
 */
@ApiResponse( description = "分页数据统一对象")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PageResponse<T> {
    /**
     * 总条目数
     */
    @Schema(description = "总条目数")
    private Long total = 0L;
    /**
     * 页尺寸
     */
    @Schema(description = "页尺寸")
    private Long pageSize = 0L;
    /**
     * 总页数
     */
    @Schema(description = "总页数")
    private Long pages = 0L;
    /**
     * 当前页码
     */
    @Schema(description = "页码")
    private Long page = 0L;
    /**
     * 数据列表
     */
    @Schema(description = "数据列表")
    private List<T> records = Collections.EMPTY_LIST;

    /**
     * 返回分页对象
     *
     * @param page mp的page对象
     */
    public PageResponse(Page page) {
        this.total = page.getTotal();
        this.pageSize = page.getSize();
        this.pages = page.getPages();
        this.page = page.getCurrent();
        this.records = page.getRecords();
    }

    /**
     * 返回分页对象
     *
     * @param page mp的page对象
     * @param list 当前页数据集合
     */
    public PageResponse(Page page, List<T> list) {
        this.total = page.getTotal();
        this.pageSize = page.getSize();
        this.pages = page.getPages();
        this.page = page.getCurrent();
        this.records = list;
    }
}